﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Serialization;

namespace Money
{
    public class GridFormatRule
    {
        private Color _color;
        private Color _selectedColor;
        private Color _fontColor;

        [XmlIgnore()]
        public Color Color
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
            }
        }
        [XmlIgnore()]
        public Color SelectedColor
        {
            get
            {
                return _selectedColor;
            }
            set
            {
                _selectedColor = value;
            }
        }
        [XmlIgnore()]
        public Color FontColor
        {
            get
            {
                return _fontColor;
            }
            set
            {
                _fontColor = value;
            }
        }

        public bool? Bold { get; set; }
        public bool? Italic { get; set; }
        public bool? Hide { get; set; }


        [XmlElement("Color")]
        public string XmlColor
        {
            get
            {
                return SerializeColor(_color);
            }
            set
            {
                _color = DeserializeColor(value);
            }
        }

        [XmlElement("SelectedColor")]
        public string XmlSelectedColor
        {
            get
            {
                return SerializeColor(_selectedColor);
            }
            set
            {
                _selectedColor = DeserializeColor(value);
            }
        }

        [XmlElement("FontColor")]
        public string XmlFontColor
        {
            get
            {
                return SerializeColor(_fontColor);
            }
            set
            {
                _fontColor = DeserializeColor(value);
            }
        }

        // TODO these are support functions and should really be located elsewhere
        public enum ColorFormat
        {
            NamedColor,
            ARGBColor
        }

        public Color DeserializeColor(string color)
        {
            Color newColor = Color.Empty;

            byte a, r, g, b;

            string[] pieces = color.Split(new char[] { ':' });

            ColorFormat colorType = (ColorFormat)
            Enum.Parse(typeof(ColorFormat), pieces[0], true);

            switch (colorType)
            {
                case ColorFormat.NamedColor:
                    if (!pieces[1].Equals("Empty"))
                    {
                        newColor = Color.FromName(pieces[1]);
                    }
                    break;

                case ColorFormat.ARGBColor:
                    a = byte.Parse(pieces[1]);
                    r = byte.Parse(pieces[2]);
                    g = byte.Parse(pieces[3]);
                    b = byte.Parse(pieces[4]);

                    newColor = Color.FromArgb(a, r, g, b);
                    break;
            }

            return newColor;
        }

        public string SerializeColor(Color color)
        {
            string xmlString = "";

            if (color.IsNamedColor)
            {
                xmlString = string.Format("{0}:{1}", ColorFormat.NamedColor, color.Name);
            }
            else if (color.IsEmpty)
            {
                xmlString = string.Format("{0}:{1}", ColorFormat.NamedColor, "Empty");
            }
            else
            {
                xmlString = string.Format("{0}:{1}:{2}:{3}:{4}", ColorFormat.ARGBColor, color.A, color.R, color.G, color.B);
            }

            return xmlString;
        }

        public GridFormatRule()
        {
            Color = Color.Empty;
            SelectedColor = Color.Empty;
            FontColor = Color.Empty;
            Bold = null;
            Italic = null;
            Hide = null;
        }
    }
}
